package com.example.manu_facturer_orders.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.manu_facturer_orders.mapper.EngineerMapper;
import com.example.manu_facturer_orders.pojo.EnginnerVO;
import com.example.manu_facturer_orders.service.EngineerService;
import commons.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: tobed
 * @date: 2022/9/12
 * @time: 11:17
 */
@Service
public class EngineerServiceImpl implements EngineerService {

    @Autowired
    EngineerMapper engineerMapper;


    /**
     * 工程师查询
     * @param userCode
     * @param userName
     * @param page
     * @return
     */
    @Override
    public Page<EnginnerVO> listEngineer(String userCode, String userName,Integer contractStatus, Page<EnginnerVO> page) {
        Page<EnginnerVO> enginnerVOPage = engineerMapper.listEnginner(userCode, userName,contractStatus, page);
        return enginnerVOPage;
    }

    /**
     * 解约工程师
     * @param id
     * @return
     */
    @Override
    public Result terminationEngineer(Integer id) {
        int i = engineerMapper.terminationEngineer(id);
        if (i!=0) {
            return Result.success();
        }else {
            return Result.error();
        }
    }

    /**
     * 无分页全查未签约
     * @return
     */
    @Override
    public Result listEngineerNoPage() {
        List<EnginnerVO> list = engineerMapper.listEngineerNoPage();
        if (list == null) {
            return Result.error();
        }else{
            return Result.success(list);
        }
    }

    /**
     * 批量签约工程师
     * @param id
     * @return
     */
    @Override
    public Result contractEngineer(String id) {
        List<String> allId = new ArrayList<>();
        String[] split = id.split(",");
        for (int i = 0; i < split.length; i++) {
            allId.add(split[i]);
        }
        int i = engineerMapper.contractEngineer(allId);
        if (i!=0){
            return Result.success();
        }else{
            return Result.error();
        }
    }

    /**
     * 启用&停用工程师
     * @param id
     * @return
     */
    @Override
    public Result stopOrOpenEngineer(Integer id,Integer contractStatus) {
        int status = 1;
        if (contractStatus.equals(1)){
            status = 2;
        }
        int i = engineerMapper.stopOrOpenEngineer(id,status);
        if (i!=0){
            return Result.success();
        }else{
            return Result.error();
        }
    }


}
